<?php

class champsignups
{
	var $mess;
	var $champid;
    
    var $vehicles = Array();
    var $divisions = Array();
    
	var $toRender = Array();

	function champsignups($champid)
	{
		$mess = '';
		$this->champid = $champid;
		if(isset($_POST['removeRecord']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']))
		{
			$this->doEdit();
		}
		$this->setupEdit();
		$this->setupAdd();
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		foreach( $this->toRender as $key => $value){
			$value->render();
		}
		
	}
	
	function doDelete()
	{
		global $db, $lang;
		
		$qUpdateChamps = 'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' WHERE user_id = ' . $_POST['driver'];
		$result = $db->sql_query($qUpdateChamps);
		if($row = $db->sql_fetchrow($result))
		{
			//get the record
			$qDel = "delete from ". CHAMPSIGNUPS_TABLE . " Where cs_champ = " . $this->champid . " AND cs_driver = " . $_POST['driver'];
			$db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			
			$qSel = 'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' WHERE user_id = ' . $_POST['driver'];
			$qUpd = "insert into  ". CHAMPSIGNUPS_TABLE . " (cs_no, cs_driver, cs_team, cs_champ, cs_div, cs_image, cs_car, cs_imageThmb, cs_entryOwner) 
					 values(" . $_POST['carNo'] . ", " . $_POST['driver'] . ", " .  $_POST['team'] . ", "  . $this->champid . ", " . 
							  $_POST['division'] . ", '', ". $_POST['car'] . ", '', " . $_POST['driver'] . ")";
			$result = $db->sql_query($qSel);
			if($row = $db->sql_fetchrow($result))
			{
				$db->sql_query($qUpd);
				$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
			}
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qSel = 'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' WHERE user_id = ' . $_POST['driver'];
			$result = $db->sql_query($qSel);
			if($row = $db->sql_fetchrow($result))
			{
				$qUpd = "update  ". CHAMPSIGNUPS_TABLE . " set cs_no = " . $_POST['carNo'] .
													  ", cs_team = " . $_POST['team'] .
													  ", cs_car = " . $_POST['car'] .
													  ", cs_div = " . $_POST['division'] .
													  " WHERE cs_entryOwner = " . $_POST['driver'] . ' AND ' .
													  " cs_champ = " . $this->champid;
				$db->sql_query($qUpd);
				
				$qUpd = "update  ". EVENTSIGNUPS_TABLE . ", " . EVENTS_TABLE . ", " . EVENTDATES_TABLE . "
						 set es_no = " . $_POST['carNo'] .
						  ", es_team = " . $_POST['team'] .
						  ", es_car = " . $_POST['car'] .
						  ", es_division = " . $_POST['division'] .
					   " WHERE es_entryOwner = " . $_POST['driver'] . " AND ed_date > NOW() AND es_event = e_id AND 
					   		   ed_event = e_id AND ed_division = " . $_POST['division'] . " AND e_championship = " . $this->champid;
				$db->sql_query($qUpd);
								
				$this->mess = new addMessage(str_replace('{entry}', $row['user_drivername'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
			}
			else
			{
				$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			}
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields()
	{
		global $db;
				
		$valid = isset($_POST['carNo']) && is_numeric($_POST['carNo'])
				&& isset($_POST['division']) && is_numeric($_POST['division'])
				&& isset($_POST['car']) && is_numeric($_POST['car'])
				&& isset($_POST['team']) && is_numeric($_POST['team'])
				&& isset($_POST['driver']) && is_numeric($_POST['driver']);
		
		return $valid;	  
	}
	
	function setupEdit()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('admin/edit_champsignups.php');
		include_once('includes/renderInclude.php');
		
		
        $q_cars = 'SELECT c_id, c_model, c_manufacturer FROM ' . CARS_TABLE . 
                    ' WHERE c_class IN (SELECT c_class FROM ' . CHAMP_CLASSES_TABLE . 
                    ' WHERE c_champid = ' . $this->champid . ') ORDER BY c_manufacturer, c_model';
        $result = $db->sql_query($q_cars);
        while($row = $db->sql_fetchrow($result)) 
        {
            $this->vehicles[] = $row;
        }
        
        
        
        $q_divisions = 'SELECT d_id, d_title FROM ' . DIVISIONS_TABLE . ' WHERE d_champid = ' . $this->champid . 
                            ' ORDER BY d_order';
        $result = $db->sql_query($q_divisions);
        while($row = $db->sql_fetchrow($result)) 
        {
            $this->divisions[] = $row;
        }
        
        //get the record
		
		$q = "SELECT *, pf_drivername as user_drivername 
			  FROM " . CHAMPSIGNUPS_TABLE . ", " . USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d 
			  WHERE u.user_id = d.user_id AND u.user_id = cs_driver AND cs_champ = " . $this->champid . 
			  " ORDER BY cs_no, pf_drivername ASC";
		$result = $db->sql_query($q);
        
        
			
        
        
		$introMess = false;
		
		while($row = $db->sql_fetchrow($result)) 
		{
			if(!$introMess)
			{
				$this->toRender[] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
				$this->toRender[] = new renderInclude('tpl/tableStart.tpl');
				$introMess = true;
			}
			
			$this->toRender[] = new edit_champsignups(false, $this->champid, $this->vehicles, $this->divisions, $row);
		}
		
		if($introMess)
		{
			$this->toRender[] = new renderInclude('tpl/tableEnd.tpl');
		}
	}

	function setupAdd()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('admin/edit_champsignups.php');
		include_once('includes/renderInclude.php');
		
		$this->toRender[] = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$this->toRender[] = new renderInclude('tpl/tableStart.tpl');
		$this->toRender[] = new edit_champsignups(true, $this->champid, $this->vehicles, $this->divisions);
		$this->toRender[] = new renderInclude('tpl/tableEnd.tpl');
		
		
	}

	
}
?>